// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ProxyDialog.java

package com.boci.common.ui.control;

import com.boci.clientapp.main.*;
import com.boci.common.ui.AbstractDialog;
import com.boci.common.util.*;
import com.boci.common.vo.ResponseVO;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

public class ProxyDialog extends AbstractDialog
    implements ActionListener
{

    public ProxyDialog(TradingClientApp owner, boolean modal, Locale pLocale)
    {
        super(owner, modal, pLocale, true);
        logger = CMLogger.getInstance();
        getContentPane().setLayout(null);
        initGUI();
        setText(pLocale);
        setSize(400, 250);
    }

    private void initGUI()
    {
        rootPanel = new JPanel();
        rootPanel.setLayout(null);
        rootPanel.setBorder(new EmptyBorder(0, 5, 0, 5));
        rootPanel.setBackground(Color.WHITE);
        ckUseProxy = new JCheckBox();
        ckUseProxy.setBounds(5, 0, 180, 23);
        ckUseProxy.setOpaque(false);
        ckUseProxy.addActionListener(this);
        rootPanel.add(ckUseProxy);
        centerPanel = new JPanel();
        centerPanel.setBounds(6, 23, 381, 152);
        centerPanel.setOpaque(false);
        centerPanel.setBorder(new EtchedBorder(1));
        centerPanel.setLayout(null);
        rootPanel.add(centerPanel);
        lblAddress = new JLabel();
        lblPort = new JLabel();
        lblUsername = new JLabel();
        lblPassword = new JLabel();
        txtAddress = new JTextField();
        txtPort = new JTextField();
        txtUsername = new JTextField();
        pwdPassword = new JPasswordField();
        centerPanel.add(lblAddress);
        centerPanel.add(lblPort);
        centerPanel.add(lblUsername);
        centerPanel.add(lblPassword);
        centerPanel.add(txtAddress);
        centerPanel.add(txtPort);
        centerPanel.add(txtUsername);
        centerPanel.add(pwdPassword);
        lblAddress.setBounds(15, 10, 140, 20);
        lblPort.setBounds(15, 35, 145, 20);
        lblUsername.setBounds(15, 60, 140, 20);
        lblPassword.setBounds(15, 85, 140, 20);
        txtAddress.setBounds(165, 10, 200, 20);
        txtPort.setBounds(165, 35, 200, 20);
        txtUsername.setBounds(165, 60, 200, 20);
        pwdPassword.setBounds(165, 85, 200, 20);
        btnOK = new JButton();
        btnOK.setBounds(110, 120, 80, 25);
        btnOK.addActionListener(this);
        centerPanel.add(btnOK);
        btnCancel = new JButton();
        btnCancel.setBounds(200, 120, 80, 25);
        btnCancel.addActionListener(this);
        centerPanel.add(btnCancel);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(rootPanel, "Center");
    }

    public void setText(Locale pLocale)
    {
        super.setText(pLocale);
        MessagesConfigurator msgConf = ClientAppLoader.getMessagesConfigurator();
        setTitle(msgConf.getUIMessages("PROXY.TITLE", pLocale));
        ckUseProxy.setText(msgConf.getUIMessages("PROXY.USE_PROXY", pLocale));
        lblAddress.setText(msgConf.getUIMessages("PROXY.ADDRESS", pLocale));
        lblPort.setText(msgConf.getUIMessages("PROXY.PORT", pLocale));
        lblUsername.setText(msgConf.getUIMessages("PROXY.USERNAME", pLocale));
        lblPassword.setText(msgConf.getUIMessages("PROXY.PASSWORD", pLocale));
        btnOK.setText(msgConf.getUIMessages("PROXY.OK", pLocale));
        btnCancel.setText(msgConf.getUIMessages("PROXY.CANCEL", pLocale));
        strNote = msgConf.getUIMessages("PROXY.CONFIRM_NOTE", pLocale);
        setFont(pLocale);
    }

    private void setFont(Locale pLocale)
    {
        ckUseProxy.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        lblAddress.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        lblPort.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        lblUsername.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        lblPassword.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        btnOK.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
        btnCancel.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
    }

    private void readProxyConfig()
    {
        try
        {
            AppConfigurator appConf = ClientAppLoader.getAppConfigurator();
            String strProxyEnabled = appConf.getProxyConfig("PROXY_ENABLED");
            if(strProxyEnabled != null)
            {
                if(strProxyEnabled.equals("Y"))
                    ckUseProxy.setSelected(true);
                else
                    ckUseProxy.setSelected(false);
                txtAddress.setText(appConf.getProxyConfig("PROXY_HOST"));
                txtPort.setText(appConf.getProxyConfig("PROXY_PORT"));
                txtUsername.setText(appConf.getProxyConfig("PROXY_USER"));
                pwdPassword.setText(appConf.getProxyConfig("PROXY_PASSWORD"));
            } else
            {
                ckUseProxy.setSelected(false);
            }
            setUseProxy(ckUseProxy.isSelected());
        }
        catch(Exception ex)
        {
            logger.severe(StringUtil.formatException(ex));
            ApplicationService.getInstance().showSysErrDialog();
        }
    }

    private void writeProxyConfig(boolean pUseProxy)
        throws Exception
    {
        AppConfigurator appConf = ClientAppLoader.getAppConfigurator();
        if(pUseProxy)
        {
            appConf.setProxyConfig("PROXY_ENABLED", "Y");
            appConf.setProxyConfig("PROXY_HOST", txtAddress.getText());
            appConf.setProxyConfig("PROXY_PORT", txtPort.getText());
            appConf.setProxyConfig("PROXY_USER", txtUsername.getText());
            appConf.setProxyConfig("PROXY_PASSWORD", String.copyValueOf(pwdPassword.getPassword()));
            appConf.saveProxyConfig();
        } else
        {
            appConf.setProxyConfig("PROXY_ENABLED", "N");
            appConf.saveProxyConfig();
        }
    }

    public void showDialog(boolean pVisible)
    {
        if(pVisible)
            readProxyConfig();
        super.showDialog(pVisible);
    }

    public void refresh(ResponseVO pResponseVO)
    {
        setDefFocus();
    }

    private void setUseProxy(boolean pUseProxyFlag)
    {
        lblAddress.setEnabled(pUseProxyFlag);
        lblPort.setEnabled(pUseProxyFlag);
        lblUsername.setEnabled(pUseProxyFlag);
        lblPassword.setEnabled(pUseProxyFlag);
        txtAddress.setEnabled(pUseProxyFlag);
        txtPort.setEnabled(pUseProxyFlag);
        txtUsername.setEnabled(pUseProxyFlag);
        pwdPassword.setEnabled(pUseProxyFlag);
        if(!pUseProxyFlag)
        {
            pwdPassword.setBackground(Color.LIGHT_GRAY);
            txtUsername.setBackground(Color.LIGHT_GRAY);
            txtPort.setBackground(Color.LIGHT_GRAY);
            txtAddress.setBackground(Color.LIGHT_GRAY);
        } else
        {
            pwdPassword.setBackground(Color.WHITE);
            txtUsername.setBackground(Color.WHITE);
            txtPort.setBackground(Color.WHITE);
            txtAddress.setBackground(Color.WHITE);
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        try
        {
            if(e.getSource().equals(ckUseProxy))
                setUseProxy(ckUseProxy.isSelected());
            else
            if(e.getSource().equals(btnOK))
            {
                if(ckUseProxy.isSelected())
                {
                    String strPassword = String.copyValueOf(pwdPassword.getPassword());
                    System.getProperties().put("https.proxySet", "true");
                    System.getProperties().put("https.proxyPort", txtPort.getText());
                    System.getProperties().put("https.proxyHost", txtAddress.getText());
                    System.getProperties().put("https.proxyUser", txtUsername.getText());
                    System.getProperties().put("https.proxyPassword", strPassword);
                } else
                {
                    System.getProperties().put("https.proxySet", "false");
                    System.getProperties().put("https.proxyPort", "");
                    System.getProperties().put("https.proxyHost", "");
                    System.getProperties().put("https.proxyUser", "");
                    System.getProperties().put("https.proxyPassword", "");
                }
                writeProxyConfig(ckUseProxy.isSelected());
                ApplicationService.getInstance().showInfoDialog(null, strNote);
                ApplicationService.getInstance().showProxyDialog(false);
            } else
            if(e.getSource().equals(btnCancel))
                ApplicationService.getInstance().showProxyDialog(false);
        }
        catch(Exception ex)
        {
            logger.severe(StringUtil.formatException(ex));
            ApplicationService.getInstance().showSysErrDialog();
        }
    }

    public HashMap getInputValue(String pActionType)
    {
        return null;
    }

    public void setDefFocus()
    {
        SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
                ckUseProxy.requestFocus();
            }
        }
);
    }

    JCheckBox ckUseProxy;
    JButton btnOK;
    JButton btnCancel;
    JLabel lblAddress;
    JLabel lblPort;
    JLabel lblUsername;
    JLabel lblPassword;
    JTextField txtAddress;
    JTextField txtPort;
    JTextField txtUsername;
    JPasswordField pwdPassword;
    JPanel rootPanel;
    JPanel centerPanel;
    String strNote;
    Logger logger;
}
